package com.hlw.hr_man.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hlw.hr_man.Model.Overtime;
import com.hlw.hr_man.service.OvertimeService;
import com.hlw.hr_man.serviceImpl.OvertimeServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @ClassName: GetCurrentEmployeeOvertimeController
 * @Description: 处理获取当前员工加班记录操作
 * @author 唐明
 * @date 2016年9月20日 上午10:10:38
 *
 */
public class GetCurrentEmployeeOvertimeController extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	OvertimeService otService = new OvertimeServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
		//获得当前用户id
		int eid = (int) req.getSession().getAttribute("employeeid");
		//获得当前页码
		int page = Integer.parseInt(req.getParameter("page"));
		//获得每页行数
		int pageSize = Integer.parseInt(req.getParameter("rows"));
		//计算开始页码
		int start = (page-1)*pageSize;
		//获得当前用户的加班列表
		List<Overtime> ots = otService.getEmployeeOvertimeByEid(eid,start,pageSize);
		//获得当前用户加班记录总数
		int ot_count = otService.getEmployeeOvertimeCount(eid);
		//将加班记录以json格式存放
		JSONObject currentEmpOts = new JSONObject();
		currentEmpOts.put("total", ot_count);
		currentEmpOts.put("rows", JSONArray.fromObject(ots));
		//将数据传回页面
		PrintWriter pw = resp.getWriter();
		pw.write(currentEmpOts.toString());
	}
}
